CGL.gl.sprite = {
	Sprite : function() {
		var Sprite = {
			image : new Image(),
			texture : null,
			isLoaded : false,
		
			load : function(src) {
				var gl = CGL.canvas.getContext();
				CGL.resources.addImageResource();
				this.image.addEventListener("load", this.loaded, false);
				this.image.src = src;
				this.texture = gl.createTexture();
			},
		
			loaded : function() {
				var gl = CGL.canvas.getContext();
				Sprite.isLoaded = true;
				CGL.resources.notifyLoadedImage();
				Sprite.init();
			},
			
			init : function() {
				var gl = CGL.canvas.getContext();
				
				gl.bindTexture(gl.TEXTURE_2D, this.texture);
				
				gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, Sprite.image);
				
				gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
				gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
				gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
				gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
				
				gl.bindTexture(gl.TEXTURE_2D, null);
			},
			
			isLoaded : function() {
				return this.loaded;
			},
			
			renderScaled : function(x, y, scale) {
				var gl = CGL.canvas.getContext();
				
				gl.uniform1i(CGL.gl.noTextureLocation, 0);
				gl.bindTexture(gl.TEXTURE_2D, this.texture);
				
				var vertexBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 x, y,
				                                                 x, y + this.image.height * scale,
				                                                 x + this.image.width * scale, y,
				                                                 x + this.image.width * scale, y + this.image.height * scale
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.positionLocation);
				gl.vertexAttribPointer(CGL.gl.positionLocation, 2, gl.FLOAT, false, 0, 0);
				
				
				var texCoordBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 0.0, 0.0,
				                                                 0.0, 1.0,
				                                                 1.0, 0.0,
				                                                 1.0, 1.0
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.texCoordLocation);
				gl.vertexAttribPointer(CGL.gl.texCoordLocation, 2, gl.FLOAT, false, 0, 0);
				
				gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
				
				gl.disableVertexAttribArray(CGL.gl.texCoordLocation);
			},
			
			render : function(x, y) {
				var gl = CGL.canvas.getContext();
				
				gl.uniform1i(CGL.gl.noTextureLocation, 0);
				gl.bindTexture(gl.TEXTURE_2D, this.texture);
				
				var vertexBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 x, y,
				                                                 x, y + this.image.height,
				                                                 x + this.image.width, y,
				                                                 x + this.image.width, y + this.image.height
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.positionLocation);
				gl.vertexAttribPointer(CGL.gl.positionLocation, 2, gl.FLOAT, false, 0, 0);
				
				
				var texCoordBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 0.0, 0.0,
				                                                 0.0, 1.0,
				                                                 1.0, 0.0,
				                                                 1.0, 1.0
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.texCoordLocation);
				gl.vertexAttribPointer(CGL.gl.texCoordLocation, 2, gl.FLOAT, false, 0, 0);
				
				gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
				
				gl.disableVertexAttribArray(CGL.gl.texCoordLocation);
			},
			
			renderSub : function(x, y, startX, startY, width, height) {
				var gl = CGL.canvas.getContext();
				
				var u1 = (startX) / this.image.width;
				var u2 = (startX + width) / this.image.width;
				var v1 = (startY) / this.image.height;
				var v2 = (startY + height) / this.image.height;
				
				gl.uniform1i(CGL.gl.noTextureLocation, 0);
				gl.bindTexture(gl.TEXTURE_2D, this.texture);
				
				var vertexBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 x, y,
				                                                 x, y + height,
				                                                 x + width, y,
				                                                 x + width, y + height
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.positionLocation);
				gl.vertexAttribPointer(CGL.gl.positionLocation, 2, gl.FLOAT, false, 0, 0);
				
				
				var texCoordBuffer = gl.createBuffer();
				gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
				gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
				                                                 u1, v1,
				                                                 u1, v2,
				                                                 u2, v1,
				                                                 u2, v2
				                                                 ]), gl.STATIC_DRAW);
				gl.enableVertexAttribArray(CGL.gl.texCoordLocation);
				gl.vertexAttribPointer(CGL.gl.texCoordLocation, 2, gl.FLOAT, false, 0, 0);
				
				gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
				
				gl.disableVertexAttribArray(CGL.gl.texCoordLocation);
			}
		};
		return Sprite;
	}	
};